/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "RenderEngine.h"
#include "BaseTypes.h"
#include "OGLError.h"

#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <gl/glext.h>


RenderEngine::RenderEngine()
:_xMinOrtho(0), _xMaxOrtho(10), _yMinOrtho(0), _yMaxOrtho(10), _curTexBound(0)
{
}

RenderEngine::~RenderEngine()
{

}

void RenderEngine::setOrtho(uint iXMinOrtho, uint iYMinOrtho, uint iXMaxOrtho, uint iYMaxOrtho)
{
   _xMinOrtho = iXMinOrtho;
   _xMaxOrtho = iXMaxOrtho;
   _yMinOrtho = iYMinOrtho;
   _yMaxOrtho = iYMaxOrtho;
}

void RenderEngine::init()
{
    glShadeModel(GL_SMOOTH); CHECK_OGL_ERROR();

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); CHECK_OGL_ERROR();
    glClearDepth(1.0f);                   CHECK_OGL_ERROR();

    glEnable(GL_DEPTH_TEST); CHECK_OGL_ERROR();
    glDepthFunc(GL_LEQUAL);  CHECK_OGL_ERROR();

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); CHECK_OGL_ERROR();
}

void RenderEngine::resize(int width, int height)
{
   glViewport(0, 0, width, height); CHECK_OGL_ERROR();

   glMatrixMode(GL_PROJECTION); CHECK_OGL_ERROR();
   glLoadIdentity();            CHECK_OGL_ERROR();

   // Calculate The Aspect Ratio Of The Window
   gluOrtho2D(_xMinOrtho, _xMaxOrtho, _yMinOrtho, _yMaxOrtho); CHECK_OGL_ERROR();

   glMatrixMode(GL_MODELVIEW); CHECK_OGL_ERROR();
   glLoadIdentity();           CHECK_OGL_ERROR();
}

void RenderEngine::BeginDraw()
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); CHECK_OGL_ERROR();
   
   glMatrixMode(GL_PROJECTION); CHECK_OGL_ERROR();
   glLoadIdentity();            CHECK_OGL_ERROR();
   gluOrtho2D(_xMinOrtho, _xMaxOrtho, _yMinOrtho, _yMaxOrtho); CHECK_OGL_ERROR();

   glMatrixMode(GL_MODELVIEW); CHECK_OGL_ERROR();
   glLoadIdentity();           CHECK_OGL_ERROR();

   glEnable(GL_TEXTURE_2D);
}


void RenderEngine::drawSprite(const GLuint & iOpenGLTextureID, const float2 & iTexCoordBL, const float2 & iTexCoordTL,
                              const float2 & iTexCoordTR, const float2 & iTexCoordBR, const quadrangle & iQuad)
{
   //if(_curTexBound != iOpenGLTextureID)
   //{
      //_curTexBound = iOpenGLTextureID;
      glBindTexture(GL_TEXTURE_2D, iOpenGLTextureID); CHECK_OGL_ERROR();
   //}

   glBegin(GL_QUADS);                            
   glTexCoord2f(iTexCoordBL.x, iTexCoordBL.y);
   glVertex2f(iQuad.pointBL.x, iQuad.pointBL.y); 

   glTexCoord2f(iTexCoordTL.x, iTexCoordTL.y);
   glVertex2f(iQuad.pointTL.x, iQuad.pointTL.y); 

   glTexCoord2f(iTexCoordTR.x, iTexCoordTR.y);
   glVertex2f(iQuad.pointTR.x, iQuad.pointTR.y); 

   glTexCoord2f(iTexCoordBR.x, iTexCoordBR.y);
   glVertex2f(iQuad.pointBR.x, iQuad.pointBR.y); 
   glEnd();                                      
   
   CHECK_OGL_ERROR();
}

uint RenderEngine::CreateTexture(unsigned char * iData, const int iWidth, const int iHeight)
{
   uint textureID = 0;

   glGenTextures(1,&textureID);             CHECK_OGL_ERROR();
   glBindTexture(GL_TEXTURE_2D, textureID); CHECK_OGL_ERROR();
   _curTexBound = textureID;

   glTexImage2D (GL_TEXTURE_2D, 0, 3, iWidth, iHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, iData); CHECK_OGL_ERROR();
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);                        CHECK_OGL_ERROR();
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);                        CHECK_OGL_ERROR();
 
   return textureID;
}

